package com.uton.rfidcontroller.viewmodel.activity_model;

import android.databinding.ObservableField;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.interfaces.INetController;
import com.uton.rfidcontroller.common.interfaces.IPublicController;
import com.uton.rfidcontroller.common.net.RetrofitFactory;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.model.CarDealerListModel;
import com.uton.rfidcontroller.model.CarDealerPatrolDetailModel;
import com.uton.rfidcontroller.viewmodel.base.BaseViewModel;

import java.util.HashMap;
import java.util.List;

/**
 * Created by 王家宝 on 2017/9/12.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public abstract class ScanActivityViewModel extends BaseViewModel implements
        IPublicController.IXRefreshController<CarDealerListModel>,
        IPublicController.ILoadingController {

    public final ObservableField<String> scanState = new ObservableField<>(Utils.getString(R.string.ui_begin_scan));
    public final ObservableField<String> preScanCount = new ObservableField<>("");

    private String carDealerId;

    public ScanActivityViewModel(String carDealerId, String carCount) {
        this.carDealerId = carDealerId;
        preScanCount.set(carCount);
        beginInit();
        refreshData();
    }

    @Override
    public void refreshData() {
        showDialog();
        HashMap<String, Object> tempData = new HashMap<>();
        tempData.put("carDealerId", carDealerId);
        go(RetrofitFactory.getInstance().carDealerPatrolDetail(tempData), new INetController<List<CarDealerPatrolDetailModel>>() {
            @Override
            public void onSuccess(List<CarDealerPatrolDetailModel> response) {
                doOnRequestComplete(response);
                dismissDialog();
            }

            @Override
            public void onError() {
                doOnRefreshError();
                dismissDialog();
            }

            @Override
            public void onComplete() {
                doOnComplete();
                dismissDialog();
            }
        });
    }

    public abstract void beginScan();

    public abstract void beginInit();

    public abstract void doOnComplete();

    public abstract void doOnRequestComplete(List<CarDealerPatrolDetailModel> data);

    public abstract boolean checkState();

    public abstract void showCheckErrorMsg();

    public abstract List getCurrentCheckStateList();

    public abstract void doOnCommitSuccess();

    public abstract void doOnCommitError();

    public abstract void doOnCommitComplete();

    public abstract void showCarDealerDetail();


    public void toCommit() {
        if (checkState()) {
            showDialog();
            go(RetrofitFactory.getInstance().submitInspectionResults(getCurrentCheckStateList()), new INetController<String>() {
                @Override
                public void onSuccess(String response) {
                    dismissDialog();
                    doOnCommitSuccess();
                }

                @Override
                public void onError() {
                    dismissDialog();
                    doOnCommitError();
                }

                @Override
                public void onComplete() {
                    dismissDialog();
                    doOnCommitComplete();
                }
            });
        } else {
            showCheckErrorMsg();
        }
    }


}
